package com.hk.rabbitmq.three;

import com.hk.rabbitmq.utils.RabbitMqUtils;
import com.hk.rabbitmq.utils.SleepUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @author: dpb
 * @date: 2022/3/14
 */
public class Work03 {

    private final static String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("C1 等待接收消息处理时间较短");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String msg = new String(delivery.getBody());

            SleepUtils.sleep(1);
            System.out.println("接收到的消息：" + msg);

            /**
             * 1、消息标记 tag
             * 2、是否批量应答未应答消息【不批量的时候效率高一点】
             */
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
        };
        //手动应答
        boolean autoAck = false;
        //设置不公平分发 0 为默认 轮训 ，1为不公平分发,2为预取值【指的是在】
//        int prefetchCount = 1;
        int prefetchCount = 5;

        channel.basicQos(prefetchCount);
        CancelCallback cancelCallback = (String consumerTag) -> {
            System.out.println(consumerTag + "消费者取消消费接口回调逻辑");
        };
        channel.basicConsume(TASK_QUEUE_NAME, autoAck, deliverCallback, cancelCallback);
    }
}
